package org.jhotdraw.samples.phototemplate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Eliazar
 */
public class Utilities {
    private static String configurationFile;
    public static String directoryTemp;
    private static Properties properties;   
    public static String separator = System.getProperty("file.separator");
    public static enum KEY{
        URL_SERVICE("URL_SERVICE"), 
        SENHA_WEB_SERVICE("SENHA_WEB_SERVICE"),
        PROXY("PROXY"),
        URL_PROXY("URL_PROXY"),
        PORTA("PORTA"),
        LOGIN("LOGIN"),
        SENHA("SENHA"),
        DIR_TEMPLATES("DIR_TEMPLATES"),
        DIR_FOTOS("DIR_FOTOS");
        
        private String  value;
        private KEY(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
                
    }
    
    public static String getConfigurationFile() {       
        return configurationFile;
    }

    public static void setConfigurationFile(String configurationFile) {
        Utilities.configurationFile = configurationFile;
    }
        
    public static void loadProperties(URI fileUri) {
        if (properties == null) {
            // o arquivo encontra-se no mesmo diretório //da aplicação
            File file = new File(fileUri);
            properties = new Properties();
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                //lê os dados que estão no arquivo
                properties.load(fis);
                fis.close();
            } catch (IOException ex) {
                
            }
        }
    }

     public static void loadProperties(InputStream inputStream) {
        if (properties == null) {
            properties = new Properties();            
            try {                
                //lê os dados que estão no arquivo               
                properties.load(inputStream);                
                inputStream.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
                ex.printStackTrace();
            }
        }
    }
    
    public static void loadProperties(String fileName) {        
        loadProperties(new File(fileName).toURI());       
    } 
    
    public static void writePropertyFile(String fileName) throws IOException {                       
        properties.store(new FileWriter(fileName), "");      
    }
     
    public static String getProperty(KEY key) {
        return properties.getProperty(key.getValue());
    }

    public static void setProperty(KEY key, String value) {
        properties.setProperty(key.getValue(), value);
    }

    public static String generateMD5(String msg) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
            byte[] array = md.digest(msg.getBytes());
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
            
        }
        return null;
    }
    
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }        
      
}
